(function() {
	var pageUtil =  {
		head : null,
		current_page : 1,
		type : 1,
		page_count : 0,
		init : function() {
			var me = this;
			me.eventCenter();
			me.loadData();
			$('.time_scope li').eq(0).addClass('selected');
		},
		loadData : function() {
			var me = this;
			$.getJSON('/user/usertradelog' , {
                "type":me.type ,
                "current_page" : me.current_page,
                'status':$(".search-status").val()
            },function (data) {
				if(data && data.error_no == 0 && data.data) {
					me.page_count = data.data.page_count;
					var trade_list = data.data.user_trade_list;
					$('.table tbody').empty();
					// if(!trade_list || trade_list.length == 0) {
					// 	var error_message = '尚未获得交易记录，快快购买，加入我们。';
					// 	var button_message = '马上购买';
					// 	if(!me.alertify) {
					// 		var Alertify = require('home:widget/ui/alertify/alertify.js');
					// 		me.alertify = new Alertify();
					// 	}
					// 	me.alertify.setMessage(error_message);
					// 	me.alertify.setButton(button_message);
					// 	me.alertify.setButtonEvent(function() {
					// 		location.href='/user/recharge';
					// 	});
					// 	me.alertify.show();
					// 	return;
					// }
					for(var index in trade_list) {
                        var el = trade_list[index];
                        var html = '<tr><td>'+trade_list[index]['serial_id']+'</td>';
                        if(trade_list[index]['pid'] == 1) {
                            html += '<td>活期</td>';
                        } else if(trade_list[index]['pid'] == 2) {
                            html += '<td>定期(30天)</td>';
                        } else if(trade_list[index]['pid'] == 3) {
                            html += '<td>定期(90天)</td>';
                        }
						html += '<td>'+new Date(trade_list[index]['timestamp'] *1000).pattern('MM-dd HH:mm:ss')+'</td>';
						var type_str = '';
						if(trade_list[index]['type'] == 1) {
							type_str = '购买';
						} else {
							type_str = '提现';
						}
						html += '<td>' + type_str + '</td>';
						html += '<td>'+mathUtil.formatAmount(trade_list[index]['amount'])+'</td>';
						html += '<td>0.00</td>';
						html += '<td>' +trade_list[index]['status']+ '</td></tr>';
						$('.table tbody').append(html);
					}
					me.loadDataComplete();
				} else {
					if(data.error_no != 0) {
						alert(data.error_message);
					}
				}
			})
		},
		loadDataComplete:function() {
			var me = this;
			if(!me.pagination) {
				var Pagination = require('home:widget/ui/pagination/pagination.js');
				me.pagination = new Pagination($('#record'), me.page_count);	
			}
		},
		renderPagination : function() {
			var me = this;
		},
		eventCenter : function() {
			var me = this;
			$('.time_scope a').click(function() {
				var text = $(this).text();
				if(text == '3天') {
					me.type = 1;
				} else if (text == '1个月') {
					me.type = 2;
				} else if (text == '3个月') {
					me.type = 3;
				} else if (text == '6个月') {
					me.type = 4;
				}
				$('.time_scope li').removeClass('selected');
				$(this).parent().addClass('selected');
				if(me.pagination) {
					me.pagination.dispose();
					me.pagination = null;
				}
				me.loadData();
 			});
 			$('#record').bind('changePage',function(event,data) {
 				me.current_page = data.goto_num;
 				me.loadData();
 			});

            $(".search-status").on("change",function(){
                if(me.pagination) {
                    me.pagination.dispose();
                    me.pagination = null;
                }
                me.current_page = 1;
                me.loadData();
            })
		}
	};
	pageUtil.init();
})();


